package com.myblog.demo.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * 配置拦截规则
 */
@Configuration
public class MyWebConfig implements WebMvcConfigurer {

    @Autowired
    private LoginInterceptor loginInterceptor;

    @Value("${imgpath}")
    private String imagePath;

    @Value("${photopath}")
    private String photoPath;

    /**
     * 映射外部图片路径
     *
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 验证码图片映射路径
        registry.addResourceHandler("/image/**").addResourceLocations("file:" + imagePath);
        // 头像映射路径
        registry.addResourceHandler("/photo/**").addResourceLocations("file:" + photoPath);
    }

    /**
     * 添加拦截器
     *
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(loginInterceptor)
                .addPathPatterns("/**") // 拦截所有的 url
                // 放开拦截的内容
                .excludePathPatterns("/login.html")
                .excludePathPatterns("/reg.html")
                .excludePathPatterns("/blog_list.html")
                .excludePathPatterns("/blog_content.html")
                .excludePathPatterns("/search_list.html")
                .excludePathPatterns("/personal_page.html")
                .excludePathPatterns("/css/**")
                .excludePathPatterns("/editor.md/**")
                .excludePathPatterns("/img/**")
                .excludePathPatterns("/image/**")
                .excludePathPatterns("/photo/**")
                .excludePathPatterns("/js/**")
                .excludePathPatterns("/user/reg")
                .excludePathPatterns("/user/login")
                .excludePathPatterns("/user/islogin")
                .excludePathPatterns("/user/logout")
                .excludePathPatterns("/user/getcode")
                .excludePathPatterns("/user/getuserinfo")
                .excludePathPatterns("/user/getuserinfobyuid")
                .excludePathPatterns("/user/get_total_rcount_and_comment")
                .excludePathPatterns("/user/is_login_user")
                .excludePathPatterns("/art/getdetail")
                .excludePathPatterns("/art/addrcount")
                .excludePathPatterns("/art/getlistbypage")
                .excludePathPatterns("/art/getcount")
                .excludePathPatterns("/art/getcountbyusername")
                .excludePathPatterns("/art/getuserinfobyartid")
                .excludePathPatterns("/art/search")
                .excludePathPatterns("/art/getlistbyuid")
                .excludePathPatterns("/comment/getcomments")
                .excludePathPatterns("/follow/get_following_list")
                .excludePathPatterns("/follow/get_follows_list")
        ;
    }
}
